﻿using Relocation.Service;
using System.Collections.Generic;
using System;
using System.Linq;
using System.Linq.Expressions;
namespace Relocation.Data
{
	public partial class Area
	{
		public Area()
			: base()
		{
			Dictionary<Type, int> _primaryKeysCache = DataModel.primaryKeysCache;
			lock (_primaryKeysCache)
			{
				int primaryKey = 0;
				if (!_primaryKeysCache.TryGetValue(this.GetType(), out primaryKey))
				{
					using (DataModel model = new DataModel())
					{
						primaryKey = model.Areas.Any() ? model.Areas.Max(c => c.area_id) : 0;
					}
				}
				_primaryKeysCache[this.GetType()] = primaryKey;
			}
		}

		static public string GetPropName(Expression<Func<Area, object>> expr)
		{
			switch (expr.Body.NodeType)
			{
				case ExpressionType.MemberAccess:
					return ((MemberExpression)expr.Body).Member.Name;
				case ExpressionType.Convert:
					return ((MemberExpression)((UnaryExpression)expr.Body).Operand).Member.Name;
				default:
					return null;
			}
		}
	}
}
